# app/db/redis.py
import json
import os
from redis import asyncio as aioredis  # ✅ 新导入方式
from app.core.config import Settings
REDIS_URL = os.getenv("REDIS_URL", Settings.REDIS_URL)
redis = aioredis.from_url(REDIS_URL, decode_responses=True)

async def get_cached_response(model, message):
    key = f"cache:{model}:{message}"
    data = await redis.get(key)
    return json.loads(data) if data else None

async def set_cached_response(model, message, reply, expire=3600):
    key = f"cache:{model}:{message}"
    await redis.set(key, json.dumps(reply), ex=expire)
